package iws.g5.uebung3.impls;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import iws.g5.uebung3.StateModel;
import iws.g5.uebung3.enums.EventType;
import iws.g5.uebung3.enums.OrganicType;
import iws.g5.uebung3.interfaces.LivingOrganicObject;
import iws.g5.uebung3.interfaces.OrganicObject;

/**
 * Cell class
 * @author t.ganzert
 *
 */
public class Cell extends LivingOrganicObject implements OrganicObject {
	static Random rand = new Random(1);
	
	double virusInfectionProbability = 0.0000002;
	double cellDivideProbabality = 0.1;
	
	public Cell(StateModel env, int birthIteration){
		super(env, birthIteration);
	}
	
	/**
	 * Event triggered by the controller
	 */
	public void onEventTriggered(int iteration, EventType event){
		if(event==EventType.CELLDIVISION){
			reproduce(iteration);
		}
		// TODO handle immunization event
	}

	@Override
	public OrganicType getType() {
		return OrganicType.CELL;
	}
	
	/**
	 * This method is reproducing the cell
	 * @param iteration number of actual iteration
	 */
	public void reproduce(int iteration){
		
		randomCellDivide(iteration);
	}

	private void randomCellDivide(int iteration) {
		int deltaX = 0;
		int deltaY = 0;
		switch(rand.nextInt(3)) {
		case 0: deltaX = +1; break;
		case 1: deltaX = -1; break;
		}
		switch(rand.nextInt(3)) {
		case 0: deltaY = +1; break;
		case 1: deltaY = -1; break;
		}
		
		int newX = xPosition + deltaX;
		int newY = yPosition + deltaY;
		if(newX>0 && newY>0 && newX<env.getXDimension() && newY<env.getYDimension() && env.isPositionEmpty(newX, newY)) {
			env.addOrganicObject(new Cell(env, iteration), newX, newY);
		}
	}
}
